package org.sgx.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

import org.sgx.dao.util.SQLUtil;
import org.sgx.dao.util.Util;

/** this offers common jdbc database connection. this impl always 
 * close connection after a sql operation ( connectionMustClose()==true)
 */
public class JDBCConnectionGiver implements ConnectionGiver {

	String url, userName, password, dbName, driver;
	Connection conn = null;	
//	private SQLUtil sql;
	
	public JDBCConnectionGiver(String url, String driver, String dbName,
			String userName, String password) throws DaoException {
		super();
		this.url = url;
		this.driver = driver;
		this.dbName = dbName;
		this.userName = userName;
		this.password = password;		
	}

	public Connection getConnection() throws DaoException {
		try {
			if(conn!=null && ! conn.isClosed())
				conn.close();
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url + dbName, userName, password);
			return conn;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public boolean connectionMustClose() {
		return true;
	}
	
}
